package triggerhelper.controllers;

import javax.swing.*;
import triggerhelper.dataHolders.Trigger;
import triggerhelper.tabs.ConditionsTab;
import triggerhelper.triggerCreator;

public class ConditionsController {

    //instance variables
    private ConditionsTab theTab;
    private triggerCreator theParent;

    private JToggleButton bHelp;
    private JTextArea tCond;

    //constructor
    public ConditionsController(ConditionsTab a, triggerCreator b) {
        theTab = a;
        theParent = b;
        setEasyAccessFields(a);
        setListeners();
    }

    private void setEasyAccessFields(ConditionsTab a) {
        bHelp = a.getbHelp();
        tCond = a.gettCond();
    }

    private void setListeners() {
        bHelp.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                help();
            }
        });
        theTab.addComponentListener(new java.awt.event.ComponentAdapter() {
            @Override
            public void componentShown(java.awt.event.ComponentEvent evt) {
                loadPane();
            }
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                saveConditions();
            }
        });

    }

    private void saveConditions() {
        //save text to current trigger
        Trigger currentTrigger = (Trigger)theParent.getCurrentTrigger();
        currentTrigger.setConditions(tCond.getText());
    }

    private void loadPane() {
        String conditions = ((Trigger)theParent.getCurrentTrigger()).getConditions();
        tCond.setText(conditions);
        tCond.setCaretPosition(conditions.length());
    }

    private void help() {
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
